#ifndef TEST_PY_GRAPH_H
#define TEST_PY_GRAPH_H

#include <string>
#include <cstdint>
#include <array>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>

namespace init {
  class Graph {
    static constexpr double eps = 1e10;//精度
  public:
    Graph() : nodeNum(0), edgeNum(0) {

    }

    //读取Mqi文件
    void readMqi(std::string &filename) {
      std::ifstream file(filename);
      if (file) {
        int num_part, node;
        file >> num_part;
        for (int i = 0; i < num_part; ++i) {
          file >> node;
          partition[node] = 1;
        }
        file.close();
      } else {
        std::cout << filename << " not found!";
        exit(0);
      }

    }

    //读取算例
    void readFile(std::string &filename) {
      std::ifstream file(filename);
      if (!file.is_open()) {
        std::cout << filename << " not found!";
        exit(0);
      }
      std::string line;
      std::getline(file, line);
      std::stringstream ss(line);
      ss >> nodeNum >> edgeNum;
      std::cout << "node num:" << nodeNum <<std::endl;
      std::cout << "edge num:" << edgeNum << std::endl;

      adj_list.resize(nodeNum);
      partition.resize(nodeNum);
      int ans = 0;
      for (int i = 0; i < nodeNum; ++i) {
        std::getline(file, line);
        std::stringstream ss(line);
        int v;
        while (ss >> v) {
          --v;
          adj_list[i].emplace_back(v);
        }
        ans += adj_list[i].size();

      }

      file.close();
    }

    double calConductance(std::string &input_file, std::string &seed_file) {
      //读取算例
      readFile(input_file);
      //读取mqi
      readMqi(seed_file);
      //计算电导
      int64_t cut = 0;
      std::array<int64_t, 2> vol = {0, 0};
      for (int i = 0; i < nodeNum; ++i) {
        vol[partition[i]] += adj_list[i].size();
        for (auto adj: adj_list[i]) {
          if (partition[i] != partition[adj]) {
            cut += 1;
          }
        }
      }
      cut /= 2;
      int64_t m_vol = std::min(vol[0],vol[1]);
      double res = static_cast<double>(cut)/static_cast<double>(m_vol);
      return res;
    }


    std::vector<std::vector<int>> adj_list;
    std::vector<int> partition;//记录分区
    int nodeNum;
    int edgeNum;
  };
}
#endif //TEST_PY_GRAPH_H
